package ciicos.service.impl;

import ciicos.base.service.SuperServiceImpl;
import ciicos.entity.LineLineEntity;
import ciicos.entity.LineLinePlaceRelationEntity;
import ciicos.entity.LinePlaceEntity;
import ciicos.mapper.LineLinePlaceRelationMapper;
import ciicos.service.LineLinePlaceRelationService;
import ciicos.service.LineLineService;
import ciicos.service.LinePlaceService;
import ciicos.util.AutoCodeUtil;
import ciicos.util.ListUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 线缆管井关联关系
 *
 * @author 企业研发中心
 * @copyright 中工互联（北京）科技集团有限公司
 * @date 2019年9月26日 上午9:18
 */
@Service
public class LineLinePlaceRelationServiceImpl extends SuperServiceImpl<LineLinePlaceRelationMapper, LineLinePlaceRelationEntity> implements LineLinePlaceRelationService {

    @Autowired
    private LinePlaceService linePlaceService;

    @Autowired
    private LineLineService lineLineService;

    @Override
    public List<LineLinePlaceRelationEntity> getReListByLineId(String lineId) {
        QueryWrapper<LineLinePlaceRelationEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(LineLinePlaceRelationEntity::getLineId,lineId);
        queryWrapper.lambda().orderByAsc(LineLinePlaceRelationEntity::getLineId,LineLinePlaceRelationEntity::getPathIndex);
        return this.list(queryWrapper);
    }

    @Override
    public List<LineLinePlaceRelationEntity> getReListByPlaceId(String placeId) {
        return AutoCodeUtil.getListByCode(this, placeId, LineLinePlaceRelationEntity::getPlaceId);
    }

    @Override
    public List<LineLinePlaceRelationEntity> getReListByLineIdList(List<String> lineIdList) {
        QueryWrapper<LineLinePlaceRelationEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().in(LineLinePlaceRelationEntity::getLineId,lineIdList);
        queryWrapper.lambda().orderByAsc(LineLinePlaceRelationEntity::getLineId,LineLinePlaceRelationEntity::getPathIndex);
        return this.list(queryWrapper);
    }

    @Override
    public List<LineLinePlaceRelationEntity> getReListByPlaceIdList(List<String> placeIdList) {
        return AutoCodeUtil.getListByCodeList(this, placeIdList, LineLinePlaceRelationEntity::getPlaceId);
    }

    @Override
    public Map<String, List<LineLinePlaceRelationEntity>> getReMapListByLineIdList(List<String> lineIdList) {
        List<LineLinePlaceRelationEntity> reList = getReListByLineIdList(lineIdList);
        Map<String, List<LineLinePlaceRelationEntity>> map = reList.stream().collect(Collectors.groupingBy(LineLinePlaceRelationEntity::getLineId));
        return map;
    }

    @Override
    public Map<String, List<LineLinePlaceRelationEntity>> getReMapListByPlaceIdList(List<String> placeIdList) {
        return AutoCodeUtil.getMapListByCodeList(this, placeIdList, LineLinePlaceRelationEntity::getPlaceId);
    }

    @Override
    public List<String> getPlaceIdListByLineId(String lineId) {
        List<String> placeIdList = getReListByLineId(lineId).stream().map(t -> t.getPlaceId()).collect(Collectors.toList());
        return placeIdList;
    }

    @Override
    public List<LinePlaceEntity> getPlaceListByLineId(String lineId) {
        List<String> placeIdList = getPlaceIdListByLineId(lineId);
        return ListUtil.isEmpty(placeIdList)?new ArrayList<>():linePlaceService.listByIds(placeIdList);
    }

    @Override
    public List<String> getLineIdListByPlaceId(String placeId) {
        List<String> lineIdList = getReListByPlaceId(placeId).stream().map(t -> t.getLineId()).collect(Collectors.toList());
        return lineIdList;
    }

    @Override
    public List<LineLineEntity> getLineListByPlaceId(String placeId) {
        List<String> lineIdList = getLineIdListByPlaceId(placeId);
        return ListUtil.isEmpty(lineIdList)?new ArrayList<>():lineLineService.listByIds(lineIdList);
    }

    @Override
    public Map<String, List<LinePlaceEntity>> getPlaceMapListByLineIdList(List<String> lineIdList) {
        List<LineLinePlaceRelationEntity> reList = getReListByLineIdList(lineIdList);
        List<String> placeIdList = ListUtil.subList(reList, LineLinePlaceRelationEntity::getPlaceId);
        List<LinePlaceEntity> placeList = ListUtil.isEmpty(placeIdList)?new ArrayList<>():linePlaceService.listByIds(placeIdList);
        Map<String, LinePlaceEntity> placeMap = placeList.stream().collect(Collectors.toMap(LinePlaceEntity::getId, Function.identity()));

        Map<String, List<LinePlaceEntity>> placeMapList = reList.stream().collect(Collectors.groupingBy(
                LineLinePlaceRelationEntity::getLineId,
                Collectors.mapping(t -> placeMap.get(t.getPlaceId()), Collectors.toList())
        ));
        lineIdList.forEach(r -> placeMapList.computeIfAbsent(r, k -> new ArrayList<>()));
        return placeMapList;
    }
}
